package com.yd.tzkk.act;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yd.tzkk.App;
import com.yd.tzkk.R;

public class MainAct extends Activity {
    private boolean loadFinish = false;
    private int pageErrCode = 0;

    private WebView webView;
    private String url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.act_main);
        super.onCreate(savedInstanceState);

        url = App.MAIN_URL+"?s="+System.currentTimeMillis();
//        String url = "file:///android_asset/www/test.html";

        webView = new WebView(this);
        webView.loadUrl(url);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                pageErrCode = 0;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                if (view.getProgress() == 100 && pageErrCode==0) {
                    loadFinish = true;
                }

                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                pageErrCode = error.getErrorCode();
            }
        });

        startDelay(0);
    }

    private void startDelay(int times){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(!loadFinish){
                    if(times%2==0){//多等待一次
                        startDelay(times+1);
                        return;
                    }
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainAct.this);
                    builder.setTitle("提示");
                    builder.setMessage("加载服务器出错！");
                    builder.setPositiveButton("重试", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            webView.loadUrl(url);
                            startDelay(times+1);
                        }
                    });
                    builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                    return;
                }
                Intent intent = new Intent(MainAct.this,WebAct.class);
                intent.putExtra("url",url);
                startActivity(intent);
                finish();
            }
        }, 3*1000);
    }
}
